package com.zhentao.login.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.dto.login.LoginDto;
import com.zhentao.login.service.impl.SysUserServiceImpl;
import com.zhentao.pojo.SysUser;
import com.zhentao.util.Enum.ApiServiceExceptionEnum;
import com.zhentao.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author "杨旭朋"
 * @ClassName: UserController
 * @date 2025年09月22日 10:27
 */
@RequestMapping("user")
@RestController
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * @title: login
     * @desc: 登陆方法
     * @params ()
     * @return: Result
     * @author: 杨旭朋
     * @date: 2025/9/22 14:59
     */
    @PostMapping("login")
    public ResultVo login(@RequestBody LoginDto loginDto) {
        SysUser one = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, loginDto.getUsername()));
        if (one != null) {
            if (!BCrypt.checkpw(loginDto.getPassword(), one.getPassword())) {
                return ResultVo.error(ApiServiceExceptionEnum.valueOf("密码错误"));
            }
            if (one.getStatus() == 2) {
                return ResultVo.error(ApiServiceExceptionEnum.valueOf("用户被禁用"));
            } else {
                return ResultVo.success("登陆成功");
            }
        }
        return ResultVo.error(ApiServiceExceptionEnum.valueOf("用户不存在"));
    }

    /**
     * @title: regin
     * @desc: 注册方法
     * @params ()
     * @return: Result
     * @author: 杨旭朋
     * @date: 2025/9/22 14:59
     */
    @PostMapping("regin")
    public ResultVo regin(@RequestBody LoginDto loginDto) {
        SysUser one = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, loginDto.getUsername()));
        if (one != null) {
            return ResultVo.error(ApiServiceExceptionEnum.valueOf("用户已存在"));
        }
        SysUser sysUser = new SysUser();
        sysUser.setUsername(loginDto.getUsername());
        sysUser.setPassword(BCrypt.hashpw(loginDto.getPassword()));
        sysUser.setRealName("用户" + loginDto.getUsername());
        sysUser.setStatus(1);
        sysUserService.save(sysUser);
        return ResultVo.success("注册成功");
    }
    /**
     * @title: getUser
     * @desc:  通过用户名获取用户信息
     * @params ()
     * @return: Result
     * @author: 杨旭朋
     * @date: 2025/9/22 15:04
     */
    @PostMapping("getUser")
    public ResultVo getUser(@RequestParam("username") String username) {
        SysUser one = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        if (one == null) {
            return ResultVo.error(ApiServiceExceptionEnum.valueOf("用户不存在"));
        } else {
            return ResultVo.success(one);
        }
    }
}
